package com.example.cdpbookstore.web.servlet;


import cn.hutool.core.util.IdUtil;
import com.example.cdpbookstore.pojo.entity.*;
import com.example.cdpbookstore.service.BookService;
import com.example.cdpbookstore.service.OrderService;
import com.example.cdpbookstore.service.impl.BookServiceImpl;
import com.example.cdpbookstore.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@WebServlet("/order-now")
public class OrderNowServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Customer customer = (Customer) request.getSession().getAttribute("customer");
        String orderId = IdUtil.randomUUID();
        LocalDateTime now = LocalDateTime.now();

        if (customer != null) {
            Long bookId = Long.valueOf(request.getParameter("id"));
            BookService bookService = new BookServiceImpl();
            Book book = bookService.getById(bookId);

            int bookQuantity = Integer.parseInt(request.getParameter("quantity"));
            if (bookQuantity <= 0) {
                bookQuantity = 1;
            }
            Order order = new Order();
            order.setId(orderId);
            order.setCustomerId(customer.getId());
            order.setCreationTime(now);
            order.setUpdateTime(now);

            List<OrderDetail> orderDetailList = new ArrayList<>();
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(IdUtil.randomUUID());
            orderDetail.setOrderId(orderId);
            orderDetail.setBookId(bookId);
            orderDetail.setNum(bookQuantity);
            orderDetail.setUnitPrice(book.getCurrentPrice());
            orderDetail.setCreationTime(now);
            orderDetail.setUpdateTime(now);
            orderDetailList.add(orderDetail);
            order.setOrderDetailList(orderDetailList);

            OrderService orderService = new OrderServiceImpl();
            boolean result = orderService.saveOrderWithDetail(order);
            if (result) {
                ArrayList<CartItem> cart = (ArrayList<CartItem>) request.getSession().getAttribute("cart");
                if (cart != null) {
                    for (CartItem item : cart) {
                        if (Objects.equals(item.getBook().getId(), bookId)) {
                            cart.remove(item);  // 清空购物车中的对应明细项
                            break;
                        }
                    }
                }
                response.sendRedirect("order.jsp");
            } else {
                out.println("订单生成失败！");
            }
        } else {
            response.sendRedirect("login.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
